package com.jiarong.controller;

import com.jiarong.domain.Role;
import com.jiarong.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 加绒
 * @date 2021/10/15 16:11
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public ModelAndView list(ModelAndView modelAndView) {
        List<Role> roleList = roleService.list();
        // 设置模型
        modelAndView.addObject("roleList", roleList);
        // 设置视图
        modelAndView.setViewName("role-list");
        return modelAndView;
    }

    @PostMapping("/save")
    public String save(Role role) {
        roleService.save(role);
        return "redirect:/role/list";
    }

    @GetMapping("/delete/{roleId}")
    public String delete(@PathVariable Long roleId) {
        roleService.delete(roleId);
        return "redirect:/role/list";
    }
}
